﻿namespace NOAH.CMS.Repository
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.InteropServices;
    using NOAH.CMS.Infracstructure;

    public class NewsArticleRepository : Repository<NewsArticle>
    {
        

        public List<NewsArticle> Filter(string filter, int categoryID, int start, int limit, out int count)
        {
            count = (from p in this.GetTable()
                     where ((filter == "") || p.Name.Contains(filter)) && ((categoryID == 0) || (p.CategoryID == categoryID))
                     select p).Count<NewsArticle>();
            return (from p in this.GetTable()
                    where ((filter == "") || p.Name.Contains(filter)) && ((categoryID == 0) || (p.CategoryID == categoryID))
                    orderby p.ID descending
                    select p).Skip<NewsArticle>(start).Take<NewsArticle>(limit).ToList<NewsArticle>();
        }


        public List<NewsArticle> Filter(string filter, int start, int limit, out int count)
        {
            List<NewsArticle> result = new List<NewsArticle>();
            if (filter.Trim() != "")
            {
                filter = Helper.XacDinhNguyenAm(filter.Trim());
                result = (from ds in dataContext.NewsArticles where ds.IsActive.HasValue && ds.IsActive.Value && (System.Data.Linq.SqlClient.SqlMethods.Like(ds.Name, "%" + filter + "%") || System.Data.Linq.SqlClient.SqlMethods.Like(ds.ShortContent, "%" + filter + "%") || System.Data.Linq.SqlClient.SqlMethods.Like(ds.Content, "%" + filter + "%") ) orderby ds.EditedOn descending select ds).ToList();
            }
            else
            {

                result = (from ds in dataContext.NewsArticles where ds.IsActive.HasValue && ds.IsActive.Value orderby ds.EditedOn descending select ds).ToList();

            }
            count = result.Count;
            return result.Skip(start).Take(limit).ToList();
        }


        public List<NewsArticle> GetByCategory(int id, int limit)
        {
            return (from p in this.GetTable()
                    where p.CategoryID == id
                    orderby p.ID descending
                    select p).Take<NewsArticle>(limit).ToList<NewsArticle>();
        }

        /// <summary>
        /// Hàm lấy tin theo chuyên mục có theo đệ qui
        /// </summary>
        /// <param name="categoryID"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public List<NewsArticle> GetByCategory(List<int> listCateID, int limit)
        {
            string strListID = ";" + string.Join(";", listCateID.ConvertAll(x => x.ToString()).ToArray()) + ";";

            List<NewsArticle> result = new List<NewsArticle>();


            result = (from ds in dataContext.NewsArticles where System.Data.Linq.SqlClient.SqlMethods.Like(strListID, "%;" + ds.CategoryID + ";%") orderby ds.ID descending select ds).ToList();

            return result.Take(limit).ToList();



        }

        /// <summary>
        /// Hàm lấy tin theo chuyên mục có theo đệ qui
        /// </summary>
        /// <param name="categoryID"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public List<NewsArticle> GetByCategoryRec(int categoryID, int limit)
        {
            List<int> listID = new List<int>();
            new NewsCategoryRepository().GetRecID(categoryID, ref listID);
            return GetByCategory(listID, limit);
        }

        /// <summary>
        /// Lấy danh sách tin bài theo chuyên mục(theo đệ qui)
        /// </summary>
        /// <param name="categoryID"></param>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<NewsArticle> GetByCategory(int categoryID, int start, int limit, out int count)
        {
            List<int> listID = new List<int>();
            new NewsCategoryRepository().GetRecID(categoryID, ref listID);
            string strListID = ";" + string.Join(";", listID.ConvertAll(x => x.ToString()).ToArray()) + ";";
            List<NewsArticle> result = new List<NewsArticle>();
            result = (from ds in dataContext.NewsArticles where System.Data.Linq.SqlClient.SqlMethods.Like(strListID, "%;" + ds.CategoryID + ";%") orderby ds.ID descending select ds).ToList();
            count = result.Count;
            return result.Skip(start).Take(limit).ToList();
        }
        /// <summary>
        /// Tăng số lần view của một bản tin!
        /// </summary>
        /// <param name="newsID"></param>
        public void IncreaseView(int newsID)
        {
            NewsArticle entity = Get(newsID);
            if (entity != null)
            {
                if (entity.View.HasValue)
                    entity.View = entity.View.Value + 1;
                else
                    entity.View = 1;
                SubmitChanges();
            }
        }

        public List<NewsArticle> GetHotNews(int limit)
        {
            return GetTable().Where(p => p.IsHotInSite == true).OrderByDescending(p => p.ID).Take(limit).ToList();
        }

    }
}

